package com.jml.account.sysUser.controller;

import com.jml.account.common.entity.utils.AjaxView;
import com.jml.account.common.entity.utils.MD5Util;
import com.jml.account.common.entity.utils.Page;
import com.jml.account.common.entity.utils.StringUtil;
import com.jml.account.sysMenu.entity.SysMenu;
import com.jml.account.sysMenu.service.SysMenuService;
import com.jml.account.sysUser.entity.SysUser;
import com.jml.account.sysUser.service.SysUserService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping(value = "/sysUser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserServiceImpl;
    @Autowired
    private SysMenuService sysMenuServiceImpl;
    /*
     * 登录
     * */
    @ResponseBody
    @RequestMapping(value = "/loginAccount")
    private AjaxView loginAccount(SysUser user,HttpServletRequest request,Model model) {
        AjaxView ajaxView = new AjaxView();

        try {
            user.setPassword(MD5Util.digest16(user.getPassword()));
            //查询sql
            user = sysUserServiceImpl.getByUserPs(user);
            HttpSession session = request.getSession();
            if(user!=null){
                session.setAttribute("user",user);
            }else{
                ajaxView.setFailed().setMessage("用户名密码错误！");
                return ajaxView;
            }

            //初始化菜单-----------------------------------------------------------------------------------------
            List<SysMenu> queryAllMenu = sysMenuServiceImpl.getAllPermission();
            SysMenu menuRoot =new SysMenu();
            Map<String,SysMenu> map = new HashMap<String,SysMenu>();
            int i = 0;
            for (SysMenu innerMenu : queryAllMenu) {
                innerMenu.setIndex(i+++"");
                map.put(innerMenu.getId(), innerMenu);

            }
            for (SysMenu sysMenu : queryAllMenu) {
                //通过子查找父
                //子菜单
                SysMenu child = sysMenu ; //假设为子菜单
                if(child.getPid() == null ){
                    menuRoot = sysMenu;
                }else{
                    //父节点
                    SysMenu parent = map.get(child.getPid());
                    parent.getChildren().add(child);
                }
            }
            session.setAttribute("permissionRoot", menuRoot);
            //-----------------------------------------------------------------------------------------



        } catch (Exception e) {
            e.printStackTrace();
            ajaxView.setFailed().setMessage("提交失败："+e.toString());
            return ajaxView;
        }
        ajaxView.setSuccess().setMessage("提交成功");
        return ajaxView;
        //return "/sysUser/userSetting";
    }

    @RequestMapping(value = "/message")
    private String  message(String id,HttpServletRequest request,Model model) {
        if(StringUtil.isEmpty(id)){//就是修改、显示自己的信息
            HttpSession session = request.getSession();
            SysUser user = (SysUser)session.getAttribute("user");
            id=user.getId();
        }
        SysUser sysUser = sysUserServiceImpl.getById(id);
        model.addAttribute("sysUser",sysUser);
        return "/sysUser/userSetting";
    }

    @RequestMapping(value = "/index")
    private String  index(HttpServletRequest request,Model model) {
        return "/sysUser/userSetting";
    }

    @ResponseBody
    @RequestMapping("/saveUserMessage")
    private AjaxView saveUserMessage(SysUser sysUser, HttpServletRequest request) {
        AjaxView ajaxView = new AjaxView();
        try {
            sysUserServiceImpl.saveUserMessage(sysUser);
        } catch (Exception e) {
            e.printStackTrace();
            ajaxView.setFailed().setMessage("提交失败："+e.toString());
            return ajaxView;
        }
        ajaxView.setSuccess().setMessage("提交成功");
        return ajaxView;
    }


    //异步请求
    @RequestMapping("/showUserIndex")
    public String showUserIndex(){
        return "/sysUser/userIndex";
    }

    /*
    * 显示用户列表
    * @param queryText 模糊查询
    * */
    @ResponseBody
    @RequestMapping(value = "/showUserList")
    public AjaxView  showUserList(@RequestParam(value="pageno",required=false,defaultValue="1") Integer pageno,
        @RequestParam(value="pagesize",required=false,defaultValue="10") Integer pagesize,
        String queryText,Model model){
        AjaxView ajaxView=new AjaxView();
        try {
            HashMap paramMap = new HashMap();
            paramMap.put("pageno", pageno);
            paramMap.put("pagesize", pagesize);

            if(StringUtil.isNotEmpty(queryText)){

                if(queryText.contains("%")){
                    queryText = queryText.replaceAll("%", "\\\\%");
                }

                paramMap.put("queryText", queryText); //
            }
            Page page = sysUserServiceImpl.queryPage(paramMap);
            model.addAttribute("page",page);
            ajaxView.put("page",page);
            ajaxView.put("pageNo",page.getPageno());
            ajaxView.setSuccess();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ajaxView;
    }

}
